<?php

namespace Morrow;

class PageController extends DefaultController {
	public function run() {

		$this->form->loadDef(
			array('myform' => array(

		            "link" => array(
		            	"type" => "text",
		            	"required" => true,
	            	),

            	)
            )
        );

		$this->form->loadDef(
			array('myform_contact' => array(

		            "showcontact" => array(
		            	"type" => "checkbox",

	            	),
		            "hidecontent" => array(
		            	"type" => "checkbox",

	            	),
		            "jiami" => array(
		            	"type" => "checkbox",

	            	),
		            "secret" => array(
		            	"type" => "hidden",

	            	),
            	)
            )
        );

		if(!$this->session->get("uid")){
			$this -> url -> redirect('home');
		}
		
		$uid=$this->session->get('uid');
		//var_dump($uid);
		$this->load('Db', $this->config->get('db'));	
		$sql=$this->db->result('
			SELECT *
			FROM webapps
			WHERE uid = ?'
			, $uid);
		//var_dump($sql["RESULT"]);

		$this->view->setContent($sql, 'data');

		$wid=$this->input->get('wid');

		//set messenger

		@$messenger=$this->session->get("messenger");

        if($messenger){

                $dollar="$";
                $messenger_content ="not difine";

                $messenger_jq="<script>".$dollar."('#messenger').fadeIn('slow');</script>";

                if($messenger=="edit1"){
                        $messenger_content="修改Webapp成功！";
                }
                if($messenger=="changelink1"){
                        $messenger_content="修改链接成功！";
                }
                if($messenger=="changelinkallready"){
                        $messenger_content="<span class='error'>链接已存在，请重新设置</span>";
                }
                if($messenger=="changelinknotyours"){
                        $messenger_content="<span class='error'>这个Webapp并非你所有</span>";
                }
                if($messenger=="delete1"){
                        $messenger_content="删除成功！";
                }
                if($messenger=="changeshowcontact1"){
                        $messenger_content="修改显示设置成功！";
                }

                $messenger_array=array('jq' => $messenger_jq, 'content' => $messenger_content );


        }
        else{
                $messenger_array=array('jq' => 'no_jq', 'content' => 'no_content' );
        }

        $this->view->setContent($messenger_array, 'messenger');

        $this->session->delete('messenger');


        //change link

        $this->form->setInput($this->input->get());

		if($this -> form -> isSubmitted('myform')) {

    	$valid = $this -> form -> validate('myform');

           $this->view->setContent($this->form->getErrors('myform'), "error");

    			if ($valid) {
        		    $formdata = $this -> form -> getValues('myform');
        				
                    $this->load('Db', $this->config->get('db'));

					$sql = $this->db->result('
					    SELECT uid
					    FROM webapps
					    WHERE wid = ?', $wid);

					$sql_new = $this->db->result('
					    SELECT uid
					    FROM webapps
					    WHERE code = ?', $formdata['link']);

					
					if( count($sql_new['RESULT']) > 0)
						{
							$this->session->set('messenger', 'changelinkallready');
							$this -> url -> redirect('edit');
							
						}
					elseif($sql['RESULT'][0]['uid']!== $uid){
							$this->session->set('messenger', 'changelinknotyours');
							$this -> url -> redirect('edit');
							
					}

					else{
						$link_code="changelink/".$formdata['link'];
						$data = array('code' => $formdata['link']);
						$this->db->update('webapps', $data, 'WHERE wid = ?', true, $wid);
						$this->session->set('messenger', 'changelink1');
						$this -> url -> redirect('edit');
					}

	        		//
	        		//
					
    			} 
    			else {
    			}
		}

		//check show contact
		$this->form->setInput($this->input->get());

		if($this -> form -> isSubmitted('myform_contact')) {

    	$valid = $this -> form -> validate('myform_contact');

    	//var_dump($formdata);

           $this->view->setContent($this->form->getErrors('myform_contact'), "error");

    			if ($valid) {
        		    $formdata = $this -> form -> getValues('myform_contact');
        		    //var_dump($formdata);
                    $this->load('Db', $this->config->get('db'));
						$this->db->updateSafe('webapps', $formdata, 'WHERE wid = ?', true, $wid);
						$this->session->set('messenger', 'changeshowcontact1');
						$this -> url -> redirect('edit');
					
					
    			} 
    			else {
    			}
		}
	}	
}